import { apiClient } from './axios'

// 工单查询参数接口
interface GetWorkOrdersParams {
  page?: number
  per_page?: number
  search?: string
  status?: string
  priority?: string
  assignee_id?: number
  creator_id?: number
  area_id?: number
  start_date?: string
  end_date?: string
  area_filter?: boolean  // 是否启用区域过滤
  show_all?: boolean     // 是否显示所有工单（管理员权限）
}

export const workOrdersApi = {
  /**
   * 获取工单列表
   * @param {GetWorkOrdersParams} params - 查询参数
   * @returns {Promise}
   */
  getWorkOrders(params: GetWorkOrdersParams = {}) {
    return apiClient.get('/work-orders', { 
      params: {
        ...params,
        area_filter: params.area_filter !== false  // 默认启用区域过滤
      }
    })
  },

  /**
   * 获取工单详情
   * @param {string|number} id - 工单ID
   * @returns {Promise}
   */
  getWorkOrder(id: string | number) {
    return apiClient.get(`/work-orders/${id}`)
  },

  /**
   * 创建工单
   * @param {Object} data - 工单数据
   * @returns {Promise}
   */
  createWorkOrder(data: any) {
    return apiClient.post('/work-orders', data)
  },

  /**
   * 更新工单
   * @param {string|number} id - 工单ID
   * @param {Object} data - 更新数据
   * @returns {Promise}
   */
  updateWorkOrder(id: string | number, data: any) {
    return apiClient.put(`/work-orders/${id}`, data)
  },

  /**
   * 删除工单
   * @param {string|number} id - 工单ID
   * @returns {Promise}
   */
  deleteWorkOrder(id: string | number) {
    return apiClient.delete(`/work-orders/${id}`)
  },

  /**
   * 获取工单统计数据
   * @returns {Promise}
   */
  getWorkOrderStats() {
    return apiClient.get('/work-orders/stats')
  },

  /**
   * 获取工单报表数据
   * @param {Object} params - 查询参数
   * @param {string} params.start_date - 开始日期
   * @param {string} params.end_date - 结束日期
   * @param {string} params.dimension - 时间维度 (day, week, month)
   * @returns {Promise}
   */
  getWorkOrderReports(params: any = {}) {
    return apiClient.get('/work-orders/analytics/reports', { params })
  },

  /**
   * 获取字段列表（包括基础字段和自定义字段）
   * @param {Object} params - 查询参数
   * @param {string} params.field_category - 字段类别 (basic, custom, all)
   * @param {boolean} params.is_active - 是否激活
   * @returns {Promise}
   */
  getFields(params: { field_category?: string; is_active?: boolean } = {}) {
    return apiClient.get('/work-orders/fields', { params })
  },

  /**
   * 获取自定义字段列表（向后兼容）
   * @param {Object} params - 查询参数
   * @returns {Promise}
   */
  getCustomFields(params: { field_category?: string; is_active?: boolean } = {}) {
    return this.getFields({ ...params, field_category: params.field_category || 'custom' })
  },

  /**
   * 创建自定义字段
   * @param {Object} data - 字段数据
   * @returns {Promise}
   */
  createCustomField(data: { [key: string]: any }) {
    return apiClient.post('/work-orders/fields', data)
  },

  /**
   * 更新自定义字段
   * @param {string|number} fieldId - 字段ID
   * @param {Object} data - 更新数据
   * @returns {Promise}
   */
  updateCustomField(fieldId: string | number, data: { [key: string]: any }) {
    return apiClient.put(`/work-orders/fields/${fieldId}`, data)
  },

  /**
   * 删除自定义字段
   * @param {string|number} fieldId - 字段ID
   * @returns {Promise}
   */
  deleteCustomField(fieldId: string | number) {
    return apiClient.delete(`/work-orders/fields/${fieldId}`)
  },

  /**
   * 获取工单处理记录
   * @param {string|number} orderId - 工单ID
   * @returns {Promise}
   */
  getProcessRecords(orderId: string | number, params?: {
    page?: number;
    per_page?: number;
    action_type?: string;
    user_id?: number;
  }) {
    return apiClient.get(`/work-orders/${orderId}/process-records`, { params })
  },

  /**
   * 创建工单处理记录
   * @param {string|number} orderId - 工单ID
   * @param {Object} data - 处理记录数据
   * @returns {Promise}
   */
  createProcessRecord(orderId: string | number, data: any) {
    return apiClient.post(`/work-orders/${orderId}/process-records`, data)
  },

  /**
   * 获取工单已使用的材料列表
   * @param {string|number} orderId - 工单ID
   * @returns {Promise}
   */
  getWorkOrderMaterials(orderId: string | number) {
    return apiClient.get(`/work-orders/${orderId}/materials`)
  },

  /**
   * 批量添加材料使用记录
   * @param {Array} data - 材料使用数据
   * @returns {Promise}
   */
  addWorkOrderMaterials(orderId: string | number, data: {
    materials: { material_id: number; quantity: number; notes?: string }[]
  }) {
    return apiClient.post(`/work-orders/${orderId}/materials`, data)
  },

  /**
   * 删除材料使用记录
   * @param {string|number} usageId - 使用记录ID
   * @returns {Promise}
   */
  updateWorkOrderMaterial(orderId: string | number, materialId: number, data: {
    quantity: number;
    notes?: string;
  }) {
    return apiClient.put(`/work-orders/${orderId}/materials/${materialId}`, data)
  },

  deleteMaterialUsage(usageId: string | number) {
    return apiClient.delete(`/material-usage/${usageId}`)
  },

  /**
   * 获取用户列表
   * @param {number} page - 页码
   * @param {number} per_page - 每页数量
   * @returns {Promise}
   */
  // 字段管理相关接口
  toggleFieldStatus(fieldId: string | number) {
    return apiClient.put(`/work-orders/fields/${fieldId}/toggle`)
  },

  reorderFields(data: { field_orders: { field_id: number; sort_order: number }[] }) {
    return apiClient.put('/work-orders/fields/reorder', data)
  },

  validateFieldValues(data: { field_values: any }) {
    return apiClient.post('/work-orders/fields/validate', data)
  },

  /**
   * 批量更新基础字段配置
   * @param {Object} data - 基础字段配置数据
   * @returns {Promise}
   */
  updateBasicFieldsConfig(data: { fields: any[] }) {
    return apiClient.put('/work-orders/fields/basic/batch-update', data)
  },

  // 工单分析接口
  getDashboardData() {
    return apiClient.get('/work-orders/dashboard')
  },

  // 工单流程接口
  updateProcessRecord(recordId: string | number, data: {
    action?: string;
    description?: string;
    notes?: string;
  }) {
    return apiClient.put(`/process-records/${recordId}`, data)
  },

  deleteProcessRecord(recordId: string | number) {
    return apiClient.delete(`/process-records/${recordId}`)
  },

  getWorkOrderTimeline(orderId: string | number) {
    return apiClient.get(`/work-orders/${orderId}/timeline`)
  },

  getProcessRecordActions() {
    return apiClient.get('/work-orders/process-records/actions')
  },

  quickAction(orderId: string | number, action: 'accept' | 'start' | 'complete' | 'cancel') {
    return apiClient.post(`/work-orders/${orderId}/quick-actions`, { action })
  }
}